﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DotNetSpeech;

namespace FingerprintAttendance
{
    public partial class ManualVerifyForm : Form
    {
        SpeechVoiceSpeakFlags SVSF = SpeechVoiceSpeakFlags.SVSFlagsAsync;
        SpVoice Speech = new SpVoice();
        public DataTable dt;

        public ManualVerifyForm()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (txtUser.Text.Trim() == "")
            {
                //MessageBox.Show("请输入账号！");
                Speech.Speak("请输入账号", SVSF);
                txtUser.Focus();
                return;
            }
            if (txtPassword.Text.Trim() == "")
            {
                //MessageBox.Show("请输入密码！");
                Speech.Speak("请输入密码", SVSF);
                txtPassword.Focus();
                return;
            }

            ManualVerifyResult ManualVerifyResult = DataOperation.ManualVerify(txtUser.Text.Trim(), txtPassword.Text.Trim());
            if (!ManualVerifyResult.success)
            {
                //MessageBox.Show("账号或密码不正确！");
                Speech.Speak("账号或密码不正确", SVSF);
                return;
            }

            #region 同一个用户在SpanMinutes分钟内进行多次考勤，只记录一次

            DataTable dt1 = DataOperation.GetMaxIdVerifyInfo(ManualVerifyResult.id);
            int SpanMinutes = int.Parse(System.Configuration.ConfigurationSettings.AppSettings["SpanMinutes"]);

            if (dt1.Rows.Count > 0)
            {
                System.TimeSpan ND = DateTime.Now - DateTime.Parse(dt1.Rows[0]["VerifyTime"].ToString());
                if (ND.TotalMinutes <= SpanMinutes)
                {
                    MessageBox.Show("你已经登记了考勤，无需多次登录！");
                    Speech.Speak("你已经登记了考勤，无需多次登录", SVSF);
                    return;
                }

            }

            #endregion

            bool success = false;

            try
            {
                success = DataOperation.UploadVerifyInfo(ManualVerifyResult.type, ManualVerifyResult.id, DateTime.Now.ToString());
                if (!success)
                {
                    Speech.Speak("数据上传失败", SVSF);
                    //return;
                }
            }
            catch (Exception ex)
            {
                Speech.Speak("数据上传出错！", SVSF);
                //return;
            }

            try
            {
                DataOperation.AddVerifyInfo(ManualVerifyResult.id, ManualVerifyResult.name,
                                            ManualVerifyResult.type, ManualVerifyResult.number,
                                            DateTime.Now, success);

                dt = DataOperation.GetMaxIdVerifyInfo(ManualVerifyResult.id);
                Speech.Speak(ManualVerifyResult.name + "欢迎光临", SVSF);
                txtUser.Text = "";
                txtPassword.Text = "";
                this.DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                Speech.Speak("数据保存出错！", SVSF);
                return;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();

        }
    }
}
